package com.sjgl.esdn.service.impl;

import com.sjgl.esdn.domain.entity.Boxroom;
import com.sjgl.esdn.domain.entity.File;
import com.sjgl.esdn.domain.entity.uc.UCMessage;
import com.sjgl.esdn.mapper.BoxroomMapper;
import com.sjgl.esdn.mapper.FileMapper;
import com.sjgl.esdn.mapper.UCMessageMapper;
import com.sjgl.esdn.service.BoxroomService;
import com.sjgl.esdn.service.FileService;
import com.sjgl.esdn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 收藏夹服务实现
 *
 * @author 缪恒铭
 * @date 2022/08/11
 */
@Service
public class BoxroomServiceImpl implements BoxroomService {
    @Autowired
    UserService userService;

    @Autowired
    FileService fileService;

    @Autowired
    UCMessageMapper ucMessageMapper;

    @Autowired
    BoxroomMapper boxroomMapper;

    @Autowired
    FileMapper fileMapper;

    @Override
    public int insertBoxroom(Long userId, String boxroomName, String intro) {
        //判断收藏夹是否存在
        if (!checkBoxroomUnique(userId, boxroomName)) {
            return -1;
        }
        //插入数据
        return boxroomMapper.insertBoxroom(userId, boxroomName, intro);
    }

    @Override
    public int deleteBoxroom(Long boxroomId) {
        return boxroomMapper.deleteBoxroom(boxroomId);
    }

    @Override
    public int updateBoxroom(Long boxroomId, String boxroomName, String intro, Long userId) {
        //命名冲突
        if (!checkBoxroomUnique(boxroomMapper.getUserId(boxroomId), boxroomName)
                && !Objects.equals(boxroomId, selectBoxroomIdByUserIdAndBoxroomName(userId, boxroomName))) {
            return -1;
        }
        return boxroomMapper.updateBoxroom(boxroomId, boxroomName, intro);
    }

    @Override
    public Boxroom selectBoxroomById(Long boxroomId) {
        return boxroomMapper.selectBoxroomById(boxroomId);
    }

    @Override
    public List<Long> selectFileIdListById(Long boxroomId) {
        return boxroomMapper.selectFileIdListById(boxroomId);
    }

    @Override
    public List<Long> selectBoxroomIdListByUserId(Long userId) {
        return boxroomMapper.selectBoxroomIdListByUserId(userId);
    }

    @Override
    public List<Boxroom> selectBoxroomListByBoxroomIdList(List<Long> boxroomIdList) {
        List<Boxroom> boxroomList = new ArrayList<>();
        for (Long boxroomId : boxroomIdList) {
            boxroomList.add(boxroomMapper.selectBoxroomById(boxroomId));
        }
        return boxroomList;
    }

    @Override
    public int addFile(Long boxroomId, Long fileId) {
        fileMapper.collectionNumAdd(fileId);
        //记录收藏log_user
        File file = fileService.selectFileById(fileId);
        Boxroom boxroom=selectBoxroomById(boxroomId);
        ucMessageMapper.insertMessage(new UCMessage(null,31L,boxroom.getUserId(),file.getCreateBy(),null,file.getFileId(),file.getIsBlog(),null));

        return boxroomMapper.addFile(boxroomId, fileId);
    }

    @Override
    public int deleteFile(Long boxroomId, Long fileId) {
        return boxroomMapper.deleteFile(boxroomId, fileId);
    }

    @Override
    public boolean checkBoxroomUnique(Long userId, String boxroomName) {
        return boxroomMapper.checkBoxroomUnique(userId, boxroomName) == 0;
    }

    @Override
    public boolean isBoxroomAndFileExist(Long boxroomId, Long fileId) {
        return boxroomMapper.isBoxroomAndFileExist(boxroomId, fileId) == 1;
    }

    @Override
    public Long getUserId(Long boxroomId) {
        return boxroomMapper.getUserId(boxroomMapper.getUserId(boxroomId));
    }

    @Override
    public Long selectBoxroomIdByUserIdAndBoxroomName(Long userId, String boxroomName) {
        return boxroomMapper.selectBoxroomIdByUserIdAndBoxroomName(userId, boxroomName);
    }
}
